<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :model="filters" :inline="true">
				<el-form-item>
					<el-input v-model="filters.keywords" placeholder="关键字" ></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getProducts">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" >新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表v-loading="listLoading"-->
		<el-table :data="products" v-loading="listLoading" highlight-current-row  style="width: 100%;">
			<!--多选框-->
			<el-table-column type="selection" width="55">
			</el-table-column>
			<!--索引值,为什么不用id,id不序号-->
			<el-table-column type="index" width="60">
			</el-table-column>
			<!--其他都设置值,只有一个不设置值就自动适应了-->
			<el-table-column prop="name" label="商品名">
			</el-table-column>
			<el-table-column prop="price" label="价格">
			</el-table-column>
			<el-table-column label="操作" width="250">
				<template scope="scope">
					<el-button size="small" type="success" @click="deal(scope.row)">下单</el-button>
					<el-button size="small"  @click="edit(scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="del(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"  :page-size="10" :total="total" style="float:right;">
			</el-pagination>
		</el-col>


		<!--下单框-->
		<el-dialog title="下单" :visible.sync="FormVisible" :close-on-click-modal="false">
			<el-form :model="productForm" label-width="80px" :rules="FormRules" ref="productForm">
				<el-form-item label="名称" prop="name">
					<el-input readonly v-model="productForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="价格" prop="price">
					<el-input readonly v-model="productForm.price" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="购买数量" prop="number">
					<el-input v-model="productForm.number" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="FormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="submit" :loading="Loading">确认</el-button>
			</div>
		</el-dialog>



	</section>
</template>

<script>
	export default {
		data() {
			return {
                listLoading:false,
                Loading:false,
				//查询对象
				filters:{
					keywords:''
				},
				page:1,//当前页,要传递到后台的
				total:0, //分页总数
                productForm:{},
			    products:[], //当前页数据
                FormVisible:false,
                FormRules: {
                    number: [
                        { required: true, message: '请输入购买数量', trigger: 'blur' }
                    ]
                },
			}
		},
		methods: {
            handleCurrentChange(curentPage){
                this.page = curentPage;
                this.getProducts();
			},
			//显示下单界面
            deal(row){
				this.FormVisible = true;
				this.productForm = Object.assign({}, row);
			},
			//下单
			submit(){
                this.$refs.productForm.validate((valid) => {
                    if (valid) {

                        //查询商品的库存
						this.$http.get("/store/store/getStore/"+this.productForm.id).then(result => {
							//判断
							let store = result.data;
							if(this.productForm.number > store){
                                this.$message({
                                    message: "购买数量超过库存，库存数量为："+store,
                                    type: 'error'
                                });
							}else{
                                this.$confirm('确认下单吗吗？', '提示', {}).then(() => {
                                    this.Loading = true;
                                    //NProgress.start();
                                    let para = Object.assign({}, this.productForm);
                                    //格式化参数TODO
                                    para = {"productId":para.id,"number":para.number};
                                    //发送请求
                                    this.$http.post("/order/order/deal",para)
                                        .then(result => {
                                            this.Loading = false;
                                            result = result.data;
                                            if(result.success){
                                                this.$message({
                                                    message: "下单成功，订单号为："+result.resultObj,
                                                    type: 'success'
                                                });
                                                //重置检验
                                                this.$refs['productForm'].resetFields();
                                                //关闭模态框
                                                this.FormVisible = false;
                                                //刷新页面
                                                this.getProducts();
                                            }else {
                                                this.Loading = false;
                                                this.$message({
                                                    message: result.message,
                                                    type: 'error'
                                                });
                                            }
                                        })
                                        .catch(result => {
                                            this.editLoading = false;
                                            this.$message({
                                                message: "系统错误，正在殴打程序员",
                                                type: 'error'
                                            });
                                        })
                                });
							}
						});


                    }
                });
			},
            //删除
            del(row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    //开启加载的模态框
                    this.listLoading = true;
                    //NProgress.start();
                    //后台地址的请求
                    this.$http.delete("/product/product/"+row.id).then((res) => {
                        this.listLoading = false;
                        if (res.data.success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        }

                        this.getProducts();
                    });
                }).catch(() => {
                });
            },
            getProducts(){
                //发送Ajax请求后台获取数据  axios
				//添加分页条件及高级查询条件
				let para = {
				    "page":this.page,
					"keyword":this.filters.keywords
				};
				this.listLoading = true; //显示加载圈
				//分页查询
                this.$http.post("/product/product/pagelist",para) //$.Post(.....)
                    .then(result=>{
                        this.total = result.data.total;
                        this.products = result.data.rows;
                        this.listLoading = false;  //关闭加载圈
                    });
				/* 查询所有
				this.$http.patch("/department",para) //$.Post(.....)
					.then(result=>{
                        console.log(result);
                        console.log(result.data);
                        this.products = result.data;
                        this.listLoading = false;  //关闭加载圈
                    });
                    */
			}
		},
		mounted() {
		    this.getProducts()
		}
	}

</script>

<style scoped>

</style>